package action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

import dao.UserDAO;
import model.UserModel;
import util.WebSiteUtil;

import java.util.List;
import java.util.Map;
/**
 * 用户相关操作action类
 */
public class UserAction extends BaseAction implements ModelDriven<UserModel>{
	
	private UserModel model = new UserModel();
	
	private List<UserModel> list;

    private String oldPass;
    private String newPass;
    private String newName;
    private String gender;
    private String newPhone;
    private String userid;

    public String getOldPass() {
        return oldPass;
    }

    public void setOldPass(String oldPass) {
        this.oldPass = oldPass;
    }

    public String getNewPass() {
        return newPass;
    }

    public void setNewPass(String newPass) {
        this.newPass = newPass;
    }

    public List<UserModel> getList() {
        return list;
    }

    public void setList(List<UserModel> list) {
        this.list = list;
    }
    

    public String getNewName() {
		return newName;
	}

	public void setNewName(String newName) {
		this.newName = newName;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getNewPhone() {
		return newPhone;
	}

	public void setNewPhone(String newPhone) {
		this.newPhone = newPhone;
	}

	public void setModel(UserModel model) {
		this.model = model;
	}
	

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	/**
     * 跳转到用户注册页面
     * @return
     */
	public String reg(){
		return "reg";
	}
    /**
     * 跳转到用户登录页面
     * @return
     */
	public String login(){
		return "login";
	}

    /**
     * 用户（或管理员）退出
     * @return
     */
	public String logout(){
		Map session = ActionContext.getContext().getSession();
        session.remove("user");
        session.clear();
		return "login";
	}

    /**
     * 用户退出
     * @return
     */
	public String dologin(){
		String userId = model.getUserId();
		UserDAO dao = new UserDAO();
		UserModel um = dao.getUserByUserId(userId);
		if(um==null||!um.getPassword().equals(this.model.getPassword())){
			addActionMessage("用户名或密码有误！");
			return "login";
		}
        Map session = ActionContext.getContext().getSession();
        session.put("user", um);
        if("1".equals(um.getRole())){
            return "manager";
        }else{
            return SUCCESS;
        }
	}

    /**
     * 新增用户操作
     * @return
     */
	public String adduser(){
		String userId = model.getUserId();
		UserDAO dao = new UserDAO();
		UserModel um = dao.getUserByUserId(userId);
		if(um!=null){
			addActionMessage("账号'"+userId+"'已经存在！");
			return "reg";
		}
		boolean rs = dao.createUser(model);
		if(!rs){
			addActionMessage("对不起，系统出现异常，请稍后重试！");
			return "reg";
		}
		return "manager";
	}

    /**
     * 跳转到更改密码页面
     * @return
     */
    public String modifyPass(){
        Map session = ActionContext.getContext().getSession();
        //如果检测到用户没有登录，则跳转到登录页面
        if(session.get("user")==null)
            return "login";
         return "modifyPass";
    }

    /**
     * 执行更改密码操作
     * @return
     */
    public String doModifyPass(){
        UserModel um = null;
        Map session = ActionContext.getContext().getSession();
        if(session.get("user")!=null)
            um = (UserModel)session.get("user");
        //如果不存在有效的登录用户，跳转至用户登录页面
        if(um==null)
            return "login";
        UserDAO dao = new UserDAO();
        um = dao.getUserByUserId(um.getUserId());
        if(um.getPassword().equals(this.getOldPass())){
            if(WebSiteUtil.isNotEmpty(this.getNewPass())){
                if(dao.updateUserPassword(um.getUserId(),this.getNewPass())){
                    addActionMessage("密码更新成功！");
                } else{
                    addActionMessage("密码更新失败！");
                }
            }

        }else{
            addActionMessage("旧密码输入有误！");
        }
        return "modifyPass";
    }

	@Override
    public UserModel getModel() {
        return model;
    }
	
	/*list出用户信息*/
	 public String modifyInfo(){
	        //如果检测到用户没有登录，则跳转到登录页面
	        if(userid==null){
	        	return "login";
	        }else{
	        	 UserDAO dao = new UserDAO();
	        	 model=dao.getUserByUserId(userid);
	        	 
	        	 System.out.println("I: "+model.getUserId()+model.getPhone());
	        	  return "modifyInfo";
	        }
	            
	        
	       
	    }
	 
	 public String doModifyInfo(){
	        UserModel um = null;
	        Map session = ActionContext.getContext().getSession();
	        if(session.get("user")!=null)
	            um = (UserModel)session.get("user");
	        //如果不存在有效的登录用户，跳转至用户登录页面
	        if(um==null)
	            return "login";
	        UserDAO dao = new UserDAO();
	        boolean flag = dao.updateUserInfo(um.getUserId(),this.getNewName(),this.getGender(),this.getNewPhone());
	        if(flag){
	        	addActionMessage("信息更新成功！");
	        }else{
	        	addActionMessage("信息更新失败！");
	        }
	        return "success";
	    }

}
